package com.kma.summer2012.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.kma.summer2012.client.ExceptionHandling.CrumbsRPCServiceException;
import com.kma.summer2012.client.services.catalogOfWorks.CrumbsCatalogOfWorksService;
import com.kma.summer2012.dao.CrumbsCatalogOfWorkDAO;
import com.kma.summer2012.exceptions.CrumbsUniversalException;
import com.kma.summer2012.facade.CatalogOfWorksFacade;
import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsCatalogOfWork;
import com.kma.summer2012.server.proxy.Proxy;
import com.kma.summer2012.shared.model.UICrumbsWorkItem;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CrumbsCatalogOfWorksServiceImpl extends RemoteServiceServlet implements CrumbsCatalogOfWorksService {

 //   private static final Log LOG = (Log) LogFactory.getLog(CrumbsCatalogOfWorksService.class);
    @Override
    public void saveWorkItem(UICrumbsWorkItem uiWork) throws CrumbsUniversalException, CrumbsRPCServiceException {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            CrumbsCatalogOfWork workItem = Proxy.getInstance().createCatalogOfWorks(uiWork);
            if (uiWork.getId() != 0) {
                factory.getCrumbsCatalogOfWorkDAO().update(workItem);
            } else {
                factory.getCrumbsCatalogOfWorkDAO().add(workItem);
            }

        } catch (Exception e) {
        //    LOG.error("Message",e);
            throw new CrumbsRPCServiceException("Saving work to DataBase failed.",e);

        }

    }

    @Override
    public Set<UICrumbsWorkItem> getAllWorks() throws CrumbsRPCServiceException {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        Set<UICrumbsWorkItem> works = new HashSet<UICrumbsWorkItem>();
        CrumbsCatalogOfWorkDAO dao = (CrumbsCatalogOfWorkDAO) factory.getCrumbsCatalogOfWorkDAO();
        try {
            for (CrumbsCatalogOfWork work : dao.getAll()) {
                if (!work.isDelete()) {
                    works.add(Proxy.getInstance().createFromCatalogOfWorks(work));
                }
            }
            System.out.println(Integer.toString(works.size()));
        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Getting list of works from DataBase failed.", e);
        }
        return works;

    }


    @Override
    public UICrumbsWorkItem getWorkById(int id) throws CrumbsRPCServiceException{
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);//TODO why get bean from Factory? not from injection
        CrumbsCatalogOfWorkDAO dao = (CrumbsCatalogOfWorkDAO) factory.getCrumbsCatalogOfWorkDAO();
        try {
            return Proxy.getInstance().createFromCatalogOfWorks(dao.getById(id));
        } catch (SQLException e) {
            throw new CrumbsRPCServiceException("Getting work from DataBase failed.", e);
        }
        catch (Throwable e){
            throw new CrumbsRPCServiceException("Getting work from DataBase failed.", e);
        }

    }


    @Override
    public List<String> getAllCategories() throws CrumbsRPCServiceException {
        ArrayList<String> list;
        CatalogOfWorksFacade facade = SpringBeansFactory.getInstance().getBeanByClass(CatalogOfWorksFacade.class);
        try {
            list = (ArrayList<String>) facade.getAllCategories();
        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Getting list of categories from DataBase failed.", e);
        }
        return list;
    }

    @Override
    public boolean checkForSubcategory(int subCategoryId, UICrumbsWorkItem element) throws CrumbsRPCServiceException {
        CatalogOfWorksFacade facade = SpringBeansFactory.getInstance().getBeanByClass(CatalogOfWorksFacade.class);
        try {
            CrumbsCatalogOfWork workItem = Proxy.getInstance().createCatalogOfWorks(element);
            return facade.checkForSubcategory(subCategoryId, workItem);

        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Checking for subCategory failed.", e);
        }
    }

    @Override
    public boolean deleteAllWorks(ArrayList<Integer> idList) throws CrumbsRPCServiceException {
        DAOFactory daoFactory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        CrumbsCatalogOfWorkDAO dao = (CrumbsCatalogOfWorkDAO) daoFactory.getCrumbsCatalogOfWorkDAO();
        boolean deletedAll = false;
        CatalogOfWorksFacade facade = SpringBeansFactory.getInstance().getBeanByClass(CatalogOfWorksFacade.class);
        try {
            HashSet<CrumbsCatalogOfWork> workSet = new HashSet<CrumbsCatalogOfWork>();
            for (int id : idList) {

                CrumbsCatalogOfWork work = dao.getById(id);
                workSet.add(work);
            }
            deletedAll = facade.deleteAllWorks(workSet);
        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Deletion of works from DataBase failed.", e);
        }
        return deletedAll;
    }


}
